package com.maxlen.camunda.controller;

import com.maxlen.camunda.entity.AttributeEntity;
import com.maxlen.camunda.service.AttributeService;
import com.tcloudit.common.core.web.domain.AjaxResult;
import com.tcloudit.common.core.web.domain.PageEntity;
import com.tcloudit.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/camunda/attr")
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    /**
     * 获取数据模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @GetMapping("/list")
    public AjaxResult list(PageEntity page) {
        TableDataInfo list = attributeService.getList(page);
        return AjaxResult.success(list);
    }

    /**
     * 获取数据模型详情
     * @param id id
     * @return 结果
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) {
        AttributeEntity attributeEntity = attributeService.info(id);
        return AjaxResult.success(attributeEntity);
    }

    /**
     * 新增数据模型
     * @param attribute 实体类
     * @return 结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody AttributeEntity attribute) {
        int rows = attributeService.add(attribute);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改数据模型
     * @param attribute 实体类
     * @return 结果
     */
    @PutMapping
    public AjaxResult edit(@RequestBody AttributeEntity attribute) {
        int rows = attributeService.edit(attribute);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除数据模型
     * @param ids 字段id集合
     * @return 结果
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        int rows = attributeService.remove(ids);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 根据字段名称获取记录数
     * @param attribute 字段名称与ID
     * @return 结果
     */
    @PostMapping("/getCountByName")
    public AjaxResult getCountByName(@RequestBody AttributeEntity attribute){
        int count = attributeService.getCountByName(attribute);
        return AjaxResult.success(count);
    }


}
